import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios';
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    AllList:[],  // 总数据
    shopList:[],  // 购物车数据
  },
  mutations: {
    renderList(state,value){
      state.AllList = value;
      console.log(value);
    },
    ShopIcon(state,value){
      let flag = state.shopList.some(item=>item.id === value.id);
      if(flag){
        state.shopList.forEach(item=>{
          if(item.id === value.id){
            item.count++;
          }
        })
      }else{
        value.count = 0;
        state.shopList.unshift(value)
      }
      
    }
  },
  actions: {
    asyncList(){
      axios.get('/list').then(res=>{
        this.commit('renderList',res.data)
      })
    }
  },
  modules: {
  },
  plugins: [createPersistedState()], // 数据持久化
})
